package com.ruoyi.loan.loan.controller;

import com.ruoyi.common.core.domain.LoanVo;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.loan.loan.domain.TbLoan;
import com.ruoyi.loan.loan.service.ITbLoanService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 * @author 杰少
 * @version 6.6.6
 * @description: TODO
 * @date 2023/1/14 12:37
 */
@Slf4j
@Api("loan-server")
@RestController
@RequestMapping("/loan")
public class TbLoanController extends BaseController {
    @Autowired
    private ITbLoanService tbLoanService;

    /**
     * 查询借款列表
     */
    @RequiresPermissions("loan:loan:list")
    @GetMapping("/list")
    public TableDataInfo list(TbLoan tbLoan)
    {
        startPage();
        List<TbLoan> list = tbLoanService.selectTbLoanList(tbLoan);
        return getDataTable(list);
    }

    /**
     * 导出借款列表
     */
    @RequiresPermissions("loan:loan:export")
    @Log(title = "借款", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TbLoan tbLoan)
    {
        List<TbLoan> list = tbLoanService.selectTbLoanList(tbLoan);
        ExcelUtil<TbLoan> util = new ExcelUtil<TbLoan>(TbLoan.class);
        util.exportExcel(response, list, "借款数据");
    }

    /**
     * 获取借款详细信息
     */
    @RequiresPermissions("loan:loan:query")
    @GetMapping(value = "/{loanId}")
    public AjaxResult getInfo(@PathVariable("loanId") Long loanId)
    {
        return success(tbLoanService.selectTbLoanByLoanId(loanId));
    }

    /**
     * 新增借款
     */
    @RequiresPermissions("loan:loan:add")
    @Log(title = "借款", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody @Valid LoanVo tbLoan)
    {
        return toAjax(tbLoanService.insertTbLoan(tbLoan));
    }

    /**
     * 修改借款
     */
    @RequiresPermissions("loan:loan:edit")
    @Log(title = "借款", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody @Valid LoanVo tbLoan)
    {
        return toAjax(tbLoanService.updateTbLoan(tbLoan));
    }

    /**
     * 删除借款
     */
    @RequiresPermissions("loan:loan:remove")
    @Log(title = "借款", businessType = BusinessType.DELETE)
    @DeleteMapping("/{loanIds}")
    public AjaxResult remove(@PathVariable Long[] loanIds)
    {
        return toAjax(tbLoanService.deleteTbLoanByLoanIds(loanIds));
    }
}